package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.lease.model.entities.ApartmentInfo;
import org.example.lease.model.enums.ReleaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.ApartmentInfoService;
import org.example.lease.vo.apartment.ApartmentDetailVo;
import org.example.lease.vo.apartment.ApartmentItemVo;
import org.example.lease.vo.apartment.ApartmentQueryVo;
import org.example.lease.vo.apartment.ApartmentSubmitVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/16 19:40
 */
@Tag(name = "公寓信息管理")
@RestController
@Slf4j
@RequestMapping("/admin/apartment")
public class ApartmentController {
    @Resource
    private ApartmentInfoService apartmentInfoService;
    @Operation(summary = "保存或者更新公寓信息")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdateApartment(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        log.info("参数中的地点信息为：{}", apartmentSubmitVo.getProvinceName());
        apartmentInfoService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "分页查询公寓信息")
    @GetMapping("/pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   ApartmentQueryVo apartmentQueryVo){
        IPage<ApartmentItemVo> page = new Page<>(current, size);
        //使用page作为参数，作用1传入需要查询的参数(current,size)作用2作为查询结果的容器。
        IPage<ApartmentItemVo> apartmentItemVoIPage = apartmentInfoService.pageApartmentItemByQuery(page, apartmentQueryVo);
        return Result.ok(apartmentItemVoIPage);
    }

    @Operation(summary = "根据id获取公寓详细信息")
    @GetMapping("/getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id){
        ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据id获取公寓详细信息")
    @DeleteMapping("/removeById")
    public Result<ApartmentDetailVo> removeApartmentInfoById(@RequestParam Long id){
        apartmentInfoService.removeApartmentInfoById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result<?> updateReleaseStatusById(@RequestParam Long id, @RequestParam("status") ReleaseStatus releaseStatus){
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId, id).set(ApartmentInfo::getIsRelease, releaseStatus));
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("/listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id){
        List<ApartmentInfo> apartmentInfoList = apartmentInfoService.list(Wrappers.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getDistrictId, id));
        return Result.ok(apartmentInfoList);
    }


}
